<%@page import="models.UserModel"%>
<%@page import="models.OrderProductModel"%>
<%@page import="java.util.TreeMap"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="models.OrderModel"%>
<%@page import="java.util.Calendar"%>
<%@page import="models.GeoModel"%>
<%@page import="admin.data.GeoData"%>
<%@page import="com.google.appengine.api.blobstore.BlobKey"%>
<%@page import="com.google.appengine.api.images.ImagesServiceFactory"%>
<%@page import="com.google.appengine.api.images.ImagesService"%>
<%@page import="java.util.Locale"%>
<%@page import="java.text.NumberFormat"%>
<%@page import="models.DealModel"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%	
	HttpSession ses = request.getSession();
	OrderModel order = (OrderModel) ses.getAttribute("user_order");	
	DealModel deal = order.getDeal();	
	List<OrderProductModel> listProduct = order.getListOrderProductObject();	
	UserModel user = (UserModel) session
			.getAttribute("user_logged_in");
	String city =(String)request.getAttribute("city");
	if(city==null){
		city = "ha-noi";
	}
	TreeMap check = new TreeMap();
	if(request.getAttribute("check")!=null){
		check = (TreeMap) request.getAttribute("check");
	}	
	ImagesService imagesService = ImagesServiceFactory.getImagesService();
	NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
%>
<jsp:include page="/layouts/header.jsp">
	<jsp:param name="language" value="1" />	
</jsp:include>	
<link rel="stylesheet" type="text/css" href="/css/ui-lightness/jquery-ui-1.8.18.custom.css" />
<script type="text/javascript" src="/js/jquery-ui-1.8.18.custom.min.js"></script>
<script type="text/javascript" src="/js/form-user.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		$("#btnPaymentLogin").click(function(){			
			$("#dialog .login").css("display","block");
			$("#dialog .register").css("display","none");
			$("#dialog").dialog("open");
			return false;
		});
		$("#btnPaymentRegister").click(function(){			
			$("#dialog .login").css("display","none");
			$("#dialog .register").css("display","block");
			$("#dialog").dialog("open");
			return false;
		});
		var unitPrice = <%=deal.getPriceDiscount()%>;		
		setTotalPrice();																		
	 	$(".unitQuantity").change(function() {	 								 		  
	 		setTotalPrice();
	 	});	 
	 	function setTotalPrice(){
	 		var total = 0;
	 		$(".unitQuantity").each(function () {
				var totalUnitPrice = unitPrice*$(this).val();	
				total += unitPrice*$(this).val();	
		 		$("#totalPrice-" + $(this).attr("id")).text(addCommas(totalUnitPrice));										
			});	
	 		$("#totalPrice").text(addCommas(total));
	 	}	 	 	
	 	function addCommas(nStr)
	 	{
	 		nStr += '';
	 		x = nStr.split('.');
	 		x1 = x[0];
	 		x2 = x.length > 1 ? '.' + x[1] : '';
	 		var rgx = /(\d+)(\d{3})/;
	 		while (rgx.test(x1)) {
	 			x1 = x1.replace(rgx, '$1' + '.' + '$2');
	 		}
	 		return x1 + x2;
	 	}	
	});
</script> 			
        	<div id="content_inner">
            	<div id="productDetailInfo" class="clearafter">
                	<div class="left">                    	
                    	<div id="paymentPage">
                            <div class="checkoutSteps">
                            <form method="post" action="/dang-ky-mua-hang/xac-nhan-don-hang">
                            <input type="hidden" name="action" value="xac-nhan-don-hang"/>                            
								<div class="step-3">
                                	<div class="stepPanel">
                                		<%
                                			if(order.getOrderStep()>0){
                                		%>
                                		<a href="/dang-ky-mua-hang/thong-tin-thanh-toan" class="backToStep1"></a>
                                		<%
                                			}
                                		%>
                                		<%
                                			if(order.getOrderStep()>1){
                                		%>
                                		<a href="/dang-ky-mua-hang/hinh-thuc-thanh-toan" class="backToStep2"></a>
                                		<%
                                			}
                                		%>
	                                	<%
                                			if(order.getOrderStep()>2){
                                		%>
                                		<a href="/dang-ky-mua-hang/xac-nhan-don-hang" class="backToStep3"></a>
                                		<%
                                			}
                                		%>	
                                		<%
                                			if(order.getOrderStep()>3){
                                		%>
                                		<a href="/dang-ky-mua-hang/dat-hang-thanh-cong" class="backToStep4"></a>
                                		<%
                                			}
                                		%>	
                                	</div>
                                	<%                                	
                                		if(session.getAttribute("success")!=null&&session.getAttribute("success").toString().length()>0){
                                	%>
                                	<div class="notification-e">
			                        	<strong>Thông báo</strong>			                        	
			                        	<%=session.getAttribute("success").toString()%>
			                            <img class="close" src="/images/icon_close.gif" alt="close" title="close">
			                        </div>
			                        <%
			                        		session.removeAttribute("success");
                                		}
			                        %>
                                    <table cellpadding="0" cellpadding="0">
                                    	<thead>
                                        	<tr>
                                            	<th>Phiếu/Sản phẩm</th>
                                                <th>Đơn giá</th>
                                                <th>Số lượng</th>
                                                <th>Thành tiền</th>
                                            </tr>
                                        </thead>
                                        <tbody>                                        
                                        <%
                                    		if(listProduct!=null&&listProduct.size()>0){                                    			
                                    			for(OrderProductModel product : listProduct){
                                    	%>
                                        	<tr>
                                            	<td><%=product.getName() %></td>
                                            	<td align="center" class="price" id="price-<%=product.getProductId()%>"><%=nf.format(deal.getPriceDiscount()) %>VND</td>
                                                <td align="center">
	                                            	<select name="<%=product.getProductId()%>" class="unitQuantity" id="<%=product.getProductId()%>">
	                                            		<%
	                                            			for(int i=0;i<=10;i++){	                                            				
	                                            				%>
	                                            					<option value="<%=i%>" <%=(i==product.getQuantity())?"selected":""%>><%=i %></option>
	                                            				<%                                                      					                                            				
	                                            			}
	                                            		%>
	                                            	</select>                                            	                                       
	                                            </td>
                                               	<td align="right">                                               	                                               		
													<strong>
                                              			<span class="unitPrice" id="totalPrice-<%=product.getProductId()%>"><%=nf.format(deal.getPriceDiscount()) %>
                                              			</span>VND
                                              		</strong>                                               		
                                              	</td>                                                
                                            </tr>
                                            <%
                                    			}
                                    		}
                                            %>                                           
                                            <tr>
                                            	<td colspan="3" align="right">Số tiền phải trả</td>
                                                <td align="right">                                                	
                                                	<strong>
                                                		<span id="totalPrice"><%=nf.format(order.getUnitPrice()) %></span>VND
                                                	</strong>                                                	
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                    <div class="infoReview">
                                    	<ul>
                                        	<li class="clearafter">
                                            	<span>Họ tên:</span> <%=order.getShipName() %>
                                                <a href="/dang-ky-mua-hang/thong-tin-thanh-toan" class="right" style="text-decoration: underline;">Chỉnh sửa</a>
                                            </li>
                                            <li class="clearafter"><span>Email:</span> <%=order.getShipMail() %></li>
                                            <li class="clearafter"><span>Điện thoại:</span> <%=order.getShipPhoneNumber() %></li>
                                            <li class="clearafter"><span class="left">Địa chỉ:</span><p class="right"><%=order.getShipAddressFull() %></p></li>
                                            <li class="clearafter"><span class="left">Ghi chú:</span><p class="right"><%=order.getNote() %></p></li>
                                        </ul>
                                    </div>
                                    <div class="infoReview">
                                    	<p>
                                    		Hình thức thanh toán: 
                                    		<%
                                    			switch(order.getPaymentType()){
                                    			case 1:
                                    				%>
                                    				Thanh toán bằng tiền mặt   
                                    				<%
                                    				break;
                                    			case 2:
                                    				%>
                                    				Thanh toán chuyển khoản   
                                    				<%
                                    				break;
                                    			} 
                                   			%>                                 		
                                    	</p>                                        
                                    </div>
                                    <div class="content-center">
                                    	<a href="/dang-ky-mua-hang/hinh-thuc-thanh-toan"><< Quay lại</a>
                                    	<%
                                    		if(user!=null){
                                   		%>
                                        <button class="button blue" type="submit">Tiếp tục</button>
                                        <%
                                    		}else{
                                    			%>
                                    			<a href="/dang-ky-mua-hang/hinh-thuc-thanh-toan" class="button blue" id="btnPaymentLogin">Đăng nhập</a>
                                    			<a href="/dang-ky-mua-hang/hinh-thuc-thanh-toan" class="button blue" id="btnPaymentRegister" >Đăng ký</a>
                                    			<%
                                    		}
                                        %>
                                    </div>                                    
                                </div>
                                </form>
                            </div>                                                        
                        </div>
                    </div>
                    <div class="right rightSideBar">
                    	<jsp:include page="/elements/right_menu_payment.jsp"/>
                    </div>
                </div>
            </div>
<jsp:include page="/layouts/footer.jsp">
    <jsp:param name="language" value="1" />
</jsp:include>